package com.example.consumer.service.strategy;

import com.example.common.Board;
import com.example.common.RandomUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public interface Strategy {
    /**
     *
     * @param board
     * @param allResult
     * @return 返回一个长度为3的数组arr，前两个表示插入的横、纵坐标，第三个数字arr[2]，通过(char)('1' + arr[2])计算出插入的char类型的值
     */
    int[] insertOneNumber(Board board, List<char[][]> allResult);

    /**
     *
     * @param board
     * @param map key由两个整数坐标组成，用@分割，代表board中未填入数字的空格，
     *            value为该位置能合法填入的所有数字
     */
    default int[] chooseFromMap(Board board, Map<String, Set<Character>> map) {
        int maxSize = map.values()
                .stream()
                .max((o1, o2) -> Integer.compare(o1.size(), o2.size()))
                .get()
                .size();
        List<String> list = new ArrayList<>();
        for (Map.Entry<String, Set<Character>> entry : map.entrySet()) {
            String key = entry.getKey();
            Set<Character> candidateChar = entry.getValue();
            if (candidateChar.size() == maxSize) {
                list.add(key);
            }
        }
        String key1 = list.get(RandomUtils.getRandomInt(list.size()));
        String[] split = key1.split("@");
        int x1 = Integer.parseInt(split[0]);
        int y1 = Integer.parseInt(split[1]);
        List<Character> candidateChar = new ArrayList<>(map.get(key1));
        Character candidate = candidateChar.get(RandomUtils.getRandomInt(candidateChar.size()));
        board.insert(x1, y1, candidate);
        return new int[]{x1, y1, candidate - '1'};
    }
}
